package com.company;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.FileWriter;

/**
 * @date : 2023-03-10 上午8:56
 * 使用缓冲写数据，1亿数据文件 888.9M，时间8.9s
 */

@Slf4j
public class P045_WriteNumberToFile {

    public static void main(String[] args) {
        String filename = "file/new.txt";
        int num = 10000;
        long start = System.nanoTime();
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filename), 65535)) {
            for (int i = 0; i < num; i++) {
                for (int j = 0; j < num; j++) {
                    bufferedWriter.write(i * num + j + System.lineSeparator());
                }
                bufferedWriter.flush();
            }
        } catch (Exception e) {
            log.info("数据写入异常", e);
        }
        long time = System.nanoTime() - start;
        log.info("输出1亿数据耗时：{} s", time / 1000.0 / 1000 / 1000);
    }


}
